package logic

import (
	"api/pkg"
	"context"
	"strconv"
	"user_srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.UserSrv.UserLogin(l.ctx, &user.UserLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	token, err := pkg.TokenHandler(strconv.FormatInt(login.UserId, 10))
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil

	}
	return &types.Response{
		Code:    200,
		Message: "用户登录成功",
		Data: map[string]interface{}{
			"Token": token,
		},
	}, nil
}
